//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class LoadingUI extends egret.Sprite {

    private jindu:egret.Bitmap;
    private saizi:egret.Bitmap;

    public constructor() {
        super();        
    }

    private textField:egret.TextField;
    private textFieldBefore:egret.TextField;
    private _value:number = 0;
    private bg:egret.Bitmap;
    private isShow:boolean = false;

    public beforeCreateView() {
        this.textFieldBefore = new egret.TextField();
        this.textFieldBefore.x = StageData.W/2 - 15;
        this.textFieldBefore.y = StageData.H/2 - 5;
        this.addChild(this.textFieldBefore);
        this.textFieldBefore.textColor = 0x52AEDC;
        this.textFieldBefore.height = 30;
        this.textFieldBefore.size = 30;
        this.textFieldBefore.fontFamily = "微软雅黑";
        this.textFieldBefore.textAlign = "center";
        this.textFieldBefore.text = "请稍后..";
    }

    public createView():void { 
        if (this.textFieldBefore.parent) {
            this.removeChild(this.textFieldBefore)
        }
        //抵制不良游戏 拒绝盗版游戏 注意自我保护 谨防上当受骗 适当游戏益脑 沉迷游戏伤身 合理安排时间 享受健康生活
        this.bg = MyLibrary.createBitmapByName("l_bg2_jpg");
        this.addChild(this.bg);
        
        this.jindu = MyLibrary.createBitmapByName("l_ui1_png",true);
        this.addChild(this.jindu);
        
        this.saizi = MyLibrary.createBitmapByName("l_shaiz_png",true);
        this.addChild(this.saizi);

        this.textField = new egret.TextField();
        this.addChild(this.textField);
        this.textField.textColor = 0x52AEDC;
        this.textField.height = 30;
        this.textField.size = 25;
        this.textField.fontFamily = "微软雅黑";
        this.textField.textAlign = "center";
        this.textField.text = "努力加载中..";

        egret.Tween.get(this.saizi,{"loop":true}).to({rotation:360},800);
        this.isShow = true;
        StageData.MainObj.addEventListener(Events.STAGE_SIZE,this.onSize,this);
        this.onSize();
    }

    private onSize(){
        this.bg.width = StageData.W;
        this.bg.height = StageData.H;

        this.jindu.x = StageData.W/2;
        this.jindu.y = StageData.H-(this.saizi.height*2) - 80;

        this.saizi.x = this.jindu.x;//StageData.W/2;
        this.saizi.y = this.jindu.y;//StageData.H-(this.saizi.height*3);

        this.textField.y = this.saizi.y+this.saizi.height - 30;
        this.textField.width = StageData.W;
    }

    public setProgress(current:number, total:number):void {
        this._value = Math.floor(current / total * 100);
        this.textField.text = "正在加载中，请稍后..."+this._value+"%";
    }

    public clear(){
        egret.Tween.removeTweens(this.saizi);
        StageData.MainObj.removeEventListener(Events.STAGE_SIZE,this.onSize,this);
    }
}
